package com.ssm;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class DomXml {
    public static void main(String[] args) {
        try {
            // 第一步：创建DOM树
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.newDocument();
            document.setXmlStandalone(true);
            // 第二步：完善树结构
            // 创建节点
            Element bookstore = document.createElement("card");
            Element book1 = document.createElement("操作");
            Element name1 = document.createElement("用户名");
            Element price1 = document.createElement("密码");
            Element when1 = document.createElement("城市代码");
            Element then1=document.createElement("社会保障卡卡号");
            Element cardNumber1 = document.createElement("社会保障号码");
            Element cardName1 = document.createElement("姓名");
            Element book2 = document.createElement("book");
            Element name2 = document.createElement("name");
            Element price2 = document.createElement("price");
            Element when2 = document.createElement("when");
            // 设置节点的属性、内容
            book1.setAttribute("from", "China");
            book2.setAttribute("from", "America");
            name1.setTextContent("411300");
            price1.setTextContent("411300vTZd18148h");
            when1.setTextContent("411600");
            then1.setTextContent("P86502299");
            cardNumber1.setTextContent("412726199609120821");
            cardName1.setTextContent("位路路");
            name2.setTextContent("JavaScript高级程序设计");
            price2.setTextContent("$18");
            when2.setTextContent("2016");
            // 将子节点添加到父节点
            book1.appendChild(name1);
            book1.appendChild(price1);
            book1.appendChild(when1);
            book1.appendChild(then1);
            book1.appendChild(cardNumber1);
            book1.appendChild(cardName1);
            book2.appendChild(name2);
            book2.appendChild(price2);
            book2.appendChild(when2);
            bookstore.appendChild(book1);
            bookstore.appendChild(book2);
            document.appendChild(bookstore);
            // 第三步：将树结构导入book.xml文件中
            TransformerFactory tff = TransformerFactory.newInstance();
            Transformer tf = tff.newTransformer();
            tf.setOutputProperty(OutputKeys.INDENT, "yes");// 节点换行
            tf.transform(new DOMSource(document), new StreamResult("E:\\paystudy\\card_client\\src\\main\\resources\\config.xml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
